package com.ophelisis.actions.menuMgmt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ophelisis.persistence.dao.CodeDAO;
import com.ophelisis.persistence.dao.MenuDAO;
import com.ophelisis.persistence.dao.MenuGroupDAO;
import com.ophelisis.persistence.domain.CodeVO;
import com.ophelisis.persistence.domain.MenuGroupVO;
import com.ophelisis.persistence.domain.MenuVO;

/** <pre>
 * Class : com.ophelisis.actions.menuMgmt.MenuMgmt / MenuMgmt.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author hlee / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2013. 07. 29.
 */
@Controller
public class MenuMgmt {
	@Autowired
	MenuGroupDAO menuGroupDAO;
	
	@Autowired
	MenuDAO menuDAO;
	
	@Autowired
	CodeDAO codeDAO;
	
	private List<MenuGroupVO> menuGroupList;
	private List<MenuVO> menuList;
	private List<CodeVO> codeList;
	private String sResult = "";
	
	private Log log = LogFactory.getLog(this.getClass());
	private static final String _LogPrefixStr = "MenuMgmt Log> ";
	
	@RequestMapping(value="menuMgmt.action")
	public ModelAndView menuMgmt(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="target", defaultValue="mmMain") String sTarget,
			@RequestParam(value="_groupId", defaultValue="0") String sMenuGroupId,
			@RequestParam(value="_menuId", defaultValue="0") String sMenuId,
			@RequestParam(value="_menuNm", defaultValue="") String sMenuNm,
			@RequestParam(value="_menuAuth", defaultValue="0") String sUserMenuAuth,
			@RequestParam(value="_prevUpCode", defaultValue="") String sPrevUpCode,
			@RequestParam(value="_prevCode", defaultValue="") String sPrevCode,
			@RequestParam(value="isNew", defaultValue="Y") String isNew,
			@ModelAttribute MenuVO menuVO,
			@ModelAttribute MenuGroupVO menuGroupVO) throws Exception {
		ModelAndView mav = new ModelAndView();

		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		log.debug(_LogPrefixStr + "|" + " MenuMgmt called !!!");
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");

		try {
			// 공통 요소
			mav.addObject("_groupId", sMenuGroupId);
			mav.addObject("_menuId", sMenuId);
			mav.addObject("_menuNm", sMenuNm);
			mav.addObject("_menuAuth", sUserMenuAuth);
			mav.addObject("_prevUpCode", sPrevUpCode);
			mav.addObject("_prevCode", sPrevCode);
			
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("codeGroupId", "1"); // 공통 코드 그룹 ID (1:메뉴 유형)
			params.put("useYn", "Y");
			
			if (sTarget.equals("mmMain")) {
				mav = returnMgmt("main", mav, params);
			} else if (sTarget.equals("mmList")) {
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("mmSaveGroup")) {
				try {
					sResult = (isNew.equals("Y") ? menuGroupDAO.setMenuGroupAdd(menuGroupVO) : menuGroupDAO.setMenuGroupModify(menuGroupVO)) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, menuGroupId=" + menuGroupVO.getMenuGroupId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("mmSaveMenu")) {
				try {
					sResult = (isNew.equals("Y") ? menuDAO.setMenuAdd(menuVO) : menuDAO.setMenuModify(menuVO)) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, menuId=" + menuVO.getMenuId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("mmDeleteGroup")) {
				try {
					sResult = menuGroupDAO.setMenuGroupDelete(menuGroupVO) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, menuGroupId=" + menuGroupVO.getMenuGroupId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("mmDeleteMenu")) {
				try {
					sResult = menuDAO.setMenuDelete(menuVO) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, menuId=" + menuVO.getMenuId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("mmExistMenu")) {
				params.clear();
				params.put("menuGroupId", menuGroupVO.getMenuGroupId());
				
				int resultCount = menuDAO.existMenu(params);
	
				mav.addObject("result", resultCount);
				
				mav.setViewName("/WEB-INF/jsp/common/ajaxResult.jsp");
			}
		} catch(Exception e) {
			e.printStackTrace();
		}

		return mav;
	}
	
	public ModelAndView returnMgmt(
			@RequestParam(value="cmd", defaultValue="list") String cmd,
			@ModelAttribute ModelAndView mav,
			Map<String, Object> params) throws Exception {
		if (cmd.equals("main")) {
			mav.setViewName("/WEB-INF/jsp/menuMgmt/menuMgmt.jsp");

			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " MenuMgmt Main-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		} else if (cmd.equals("list")) {
			try {
				menuGroupList = menuGroupDAO.getMenuGroupList(params);
				menuList = menuDAO.getMenuList(params);
				codeList = codeDAO.getCodeList(params);
			} catch (Exception e) {
				log.error("DB 정보 획득 failed!", e);
			}

			mav.addObject("menuGroupList", menuGroupList);
			mav.addObject("menuList", menuList);
			mav.addObject("codeList", codeList);
			mav.setViewName("/WEB-INF/jsp/menuMgmt/menuMgmtList.jsp");

			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " MenuMgmt List-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		}
		
		return mav;
	}
}

